package com.xinsoft.controller.basic;

import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.SysClientDTO;
import com.xinsoft.entity.dto.SysClientPageParam;
import com.xinsoft.entity.vo.SysClientVo;
import com.xinsoft.service.SysClientService;
import com.xinsoft.service.SysDocumentNumService;
import com.xinsoft.utils.ExcelUtils;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/system/client")
public class SysClientController {

    @Autowired
    private SysClientService sysClientService;

    @Autowired
    private SysDocumentNumService sysDocumentNumService;

    /**
     * 新增
     */
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    public Response<?> add(@RequestBody SysClientDTO sysClientDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(sysClientDTO);

        sysDocumentNumService.incrementLastSerialByDocumentId(Const.DOCUMENT_ID.CLIENT_DOCUMENT);

        return Response.succeed(sysClientService.addSysClient(sysClientDTO));
    }

    /**
     * 更新
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/edit")
    public Response<?> edit(@RequestBody  SysClientDTO sysClientDTO) {
        // 参数校验
        ValidatorUtils.validateEntity(sysClientDTO);
        return Response.succeed(sysClientService.editSysClient(sysClientDTO));
    }

    /**
     * 获取客户
     */
    @GetMapping("/detail")
    public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
        return Response.succeed(sysClientService.getSysClient(id));
    }

    /**
     * 删除
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/del")
    public Response<?> del(@RequestBody List<Integer> ids) {
        sysClientService.del(ids);
        return Response.succeed();
    }

    /**
     * 启用
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/enable")
    public Response<?> enable(@RequestBody List<Integer> ids) {
        sysClientService.enable(ids);
        return Response.succeed();
    }

    /**
     * 禁用
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/disable")
    public Response<?> disable(@RequestBody List<Integer> ids) {
        sysClientService.disable(ids);
        return Response.succeed();
    }


    /**
     * 审核
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/audit")
    public Response<?> audit(@RequestBody List<Integer> ids) {
        sysClientService.audit(ids);
        return Response.succeed();
    }

    /**
     * 反审核
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/deAudit")
    public Response<?> deAudit(@RequestBody List<Integer> ids) {
        sysClientService.deAudit(ids);
        return Response.succeed();
    }

    /**
     * 导入
     */
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/import")
    @ResponseBody
    public Response<?> importExcel(@RequestParam("file") MultipartFile multipartFile) throws IOException {
        return Response.succeed(sysClientService.importExcel(multipartFile));
    }

    @ApiOperation("导出")
    @PostMapping("/export")
    public Response<?> export(@RequestBody @ApiParam("查询参数") SysClientPageParam sysClientPageParam, HttpServletResponse response) throws Exception {
        List<SysClientVo> sysClientDTOList = sysClientService.findListByParam(sysClientPageParam);
        ExcelUtils.exportByEasypoi(sysClientDTOList, SysClientDTO.class, "客户", "客户", response, "客户");
        return Response.succeed();
    }

    /**
     * 分页
     */
    @PostMapping("/pageList")
    public Response<?> pageList(@RequestBody SysClientPageParam sysClientPageParam) {
        return Response.succeed(sysClientService.findPageListByParam(sysClientPageParam));
    }
}
